<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/
	
	/**
	 * This class handles FTP connections to the server.  FTP connections are
	 * necessary for some file management operations in the admin panel.
	 */
	class lib_ftp {
		const E_CONNECTION_FAILURE = 1;
		const E_LOGIN_FAILURE = 2;
		
		private $con;
		private $curdir;
	
		/**
		 * Open an FTP connection and login
		 *
		 * @param string $host The FTP host to connect to
		 * @param string $user The FTP username
		 * @param string $pass The FTP password
		 */
		public function __construct($host,$user,$pass){
			if(!($this->con = @ftp_connect($host)))
				throw new Exception($GLOBALS['LANG']->lang_admin->invalid_ftp_host,self::E_CONNECTION_FAILURE);
			elseif(!@ftp_login($this->con,$user,$pass))
				throw new Exception($GLOBALS['LANG']->lang_admin->ftp_login_failure,self::E_LOGIN_FAILURE);
			
			@ftp_pasv($this->con,true);
			$this->curdir = '';
		}
		
		/**
		 * Change the current directory
		 *
		 * @param string $dir The new directory to go to
		 */
		public function chdir($dir){
			$this->curdir = $dir;
		}
		
		/**
		 * Get a list of files in the current directory
		 *
		 * @return An array containing the names of the files in the directory
		 */
		public function filelist(){
			return ftp_nlist($this->con,$this->curdir);
		}
		
		/**
		 * Writes a file to the server over the FTP connection
		 *
		 * @param string $file The name of the file to write
		 * @param string $data The data to write to the file
		 * @return True if the file was written, false if it was not.
		 */
		public function fwrite($file,$data){
			$uf = tmpfile();
			fwrite($uf,$data);
			fseek($uf,0);
			$exec = @ftp_fput($this->con,$this->curdir.'/'.$file,$uf,FTP_BINARY);
			fclose($uf);
			return $exec;
		}
		
		/**
		 * Creates a new directory over the FTP connection
		 *
		 * @param string $dir The name of the directory to create
		 * @return True if the directory was created, false if it was not
		 */
		public function mkdir($dir){
			return ftp_mkdir($this->con,$this->curdir.'/'.$dir);
		}
		
		/**
		 * Deletes a file or directory over the FTP connection
		 *
		 * @param string $todel The path to the file or directory to delete
		 */
		public function delete($todel){
			if(ftp_chdir($this->con,$this->curdir.'/'.$todel)){
				// Is a directory
				$list = ftp_nlist($this->con,$this->curdir.'/'.$todel);
				foreach($list as $file){
					if($file == '.' || $file == '..') continue;
					$this->delete($todel.'/'.$file);
				}
				
				ftp_rmdir($this->con,$this->curdir.'/'.$todel);
			}else{
				// Is a file
				ftp_delete($this->con,$this->curdir.'/'.$todel);
			}
		}
		
		/**
		 * Change the permission mode on a file
		 *
		 * @param string $file The name of the file or directory to change permissions for
		 * @param int $mode The mode (given in octal of the permissions to set)
		 */
		public function chmod($file,$mode){
			return ftp_chmod($this->con,$mode,$this->curdir.'/'.$file);
		}
		
		/**
		 * End this FTP session
		 */
		public function close(){
			ftp_close($this->con);
		}

	}

?>